

////////////////////////////////////////////////////////////////////////////
//Multiplexer configuration
// D0 = P10.5, D1 = P10.6, D2 = P10.7
//Multiplexer channel 1~8
////////////////////////////////////////////////////////////////////////////
#define MUX_PORT_OUT      P10OUT
#define MUX_PORT_DIR      P10DIR
#define MUX_PORT_SEL      P10SEL
#define MUX_D0            BIT5
#define MUX_D1            BIT6
#define MUX_D2            BIT7
void InitMultiplexer();
void setMuxChannel(unsigned char channel);

////////////////////////////////////////////////////////////////////////////
//LED Display configuration
// LED01 ~ LED08 = P1.0 ~ P1.7, LED09 ~ LED11 = P2.0 ~ P2.2
////////////////////////////////////////////////////////////////////////////
#define LED_PORT1_OUT			P1OUT
#define LED_PORT1_DIR			P1DIR
#define LED_PORT1_SEL			P1SEL
#define LED_PORT1_DS			P1DS
#define LEFT_ARROW_LED		BIT1	//schematic name: LED01
#define RIGHT_ARROW_LED		BIT0	//schematic name: LED02
//#define LED1							BIT2	//schematic name: LED03 
//#define LED2							BIT3	//schematic name: LED04
//#define LED3							BIT4	//schematic name: LED05
//#define LED4							BIT5	//schematic name: LED06
//#define LED5							BIT6	//schematic name: LED07
//#define LED6							BIT7	//schematic name: LED08

#define LED_PORT2_OUT			P2OUT
#define LED_PORT2_DIR			P2DIR
#define LED_PORT2_SEL			P2SEL
#define LED_PORT2_DS			P2DS
//#define LED7							BIT0	//schematic name: LED09
//#define LED8							BIT1  //schematic name: LED10
//#define LED9							BIT2  //schematic name: LED11

#define Green_LED		BIT7	//schematic name: LED08
#define Red_LED		  BIT6	//schematic name: LED07
#define Yellow_LED  BIT5	//schematic name: LED06

enum sideLed
{
  leftSideLED,
  rightSideLED
};

enum LED_Number
{
  nLED1,
  nLED2,
  nLED3,
  nLED4,
  nLED5,
  nLED6,
  nLED7,
  nLED8,
  nLED9
};

void InitLEDDisplay();
void showSideLEDs(char side);
void setSideLEDs(char side, int led_data);
void setLED(char side, int led_number, char led_enable);
void showAllLEDsOn(char enable);
void setLEDLocation(char row, char column, char led_enable);
void showLEDs(int ledStatus);
void marqueeLight(char side, char darkLedNum);
void showLEDsInSideFail(char enable,char leftSideFailFlag, char rightSideFailFlag);
void setEmptyLED(char OnOFF);
void setGreenLED(char OnOFF);
void setRedLED(char OnOFF);
void setYellowLED(char OnOFF);


////////////////////////////////////////////////////////////////////////////
//Balance Control configuration
// B1 = P8.1/TA0.1, B2 = P8.2/TA0.2, B3 = P8.3/TA0.3
////////////////////////////////////////////////////////////////////////////
#define PWM_PERIOD      64        // PWM Period
#define BLINK_COUNT     16328     // counting about ~1s.

#define BCTRL_PORT_OUT      P8OUT
#define BCTRL_PORT_DIR      P8DIR
#define BCTRL_PORT_SEL      P8SEL
#define BCTRL1            BIT1
#define BCTRL2            BIT2
#define BCTRL3            BIT3
#define NumOfBCTRL        3

// for PWM function
#define T_CCTL0       TA0CCTL0        // CCR0 Mode
#define T_CCR0 				TA0CCR0          // PWM Period/2              

#define T_CCTL1				TA0CCTL1         // CCR1 Mode                        
#define T_CCR1 				TA0CCR1          // CCR1 PWM duty cycle base on CCR0
#define T_CCTL2				TA0CCTL2         // CCR2 Mode                        
#define T_CCR2 				TA0CCR2          // CCR2 PWM duty cycle base on CCR0
#define T_CCTL3				TA0CCTL3         // CCR3 Mode                        
#define T_CCR3 				TA0CCR3          // CCR3 PWM duty cycle base on CCR0
#define T_CTL  				TA0CTL           // SMCLK, up-down mode, clear TAR 

void InitBalanceControl();
void setBalanceChannel(unsigned char channel, char balance_enable);
char getBalanceStatus();
void InitBalanceControlByPWM();
char getChannelBalanceStatus(unsigned char channel);
char getBalanceChannel();

////////////////////////////////////////////////////////////////////////////
//AddressGetting configuration
// P4 for I2C Slave address
// getting address from P4.0~P4.6 (7bits), P4.7 is ignored.
////////////////////////////////////////////////////////////////////////////
#define ADDS_PORT_IN      P4IN
#define ADDS_PORT_DIR     P4DIR
#define ADDS_PORT_SEL     P4SEL
void InitAddressGetting();
char getAddress();
char getSlaveAddressChangeMode();

////////////////////////////////////////////////////////////////////////////
// Adc Reader configuration
// channel A0 : System Current sense (+/-)
// channel A1 : reserved for System Current sense (+/-)
// channel A2 : reserved.
// channel A3 : Temperature 1
// channel A4 : Temperature 2
// channel A5 : Temperature 3
// channel A6 : Temperature 4
// channel A7 : Temperature 5
////////////////////////////////////////////////////////////////////////////
#define Num_of_Results      8
#define ADC_PORT_SEL        P6SEL

#define Failure_Counts      8     // if failure, retry count
// 
#define DSG_Protection      4000     // discharge current protection ADC value
#define Current_Zero        2046     // middle of current ADC value between charge and discharge 
#define CHG_Protection      100      // charge current protection ADC value 

#define Temp_H_Protection   8    // temperature high protection ADC value
#define Temp_L_Protection   100    // temperature protection ADC value

void InitAdcReader();
void startAdcConversion();
void stopAdcConversion();
unsigned int getSystemCurrentADC();
unsigned int getCHGCurrentADC();
unsigned int getDSGCurrentADC();
unsigned int getESSModuleVoltage();
unsigned int getTemp1ADC();
unsigned int getTemp2ADC();
unsigned int getTemp3ADC();
unsigned int getTemp4ADC();

////////////////////////////////////////////////////////////////////////////
//MosFET Control configuration 
// P9.7 CHG control
// P9.6 DSG control
////////////////////////////////////////////////////////////////////////////
#define MOSFET_PORT_OUT      P9OUT
#define MOSFET_PORT_DIR      P9DIR
#define MOSFET_PORT_SEL      P9SEL
#define MOSFET_CHG            BIT7
#define MOSFET_DSG            BIT6
void InitMosControl();
void setMosCHG(char enable);
void setMosDSG(char enable);
char getMosCHG();
char getMosDSG();

////////////////////////////////////////////////////////////////////////////
// Calendar configuration
// Real Time Clock Workaround 
//
////////////////////////////////////////////////////////////////////////////
//struct Time { int Year, Month, DayOfWeek, Day, Hour, Minute, Second; };

//#define _BCD_MODE_
extern int SetRTCYEAR(int year); 	
extern int SetRTCMON(int month);
extern int SetRTCDAY(int day);
extern int SetRTCDOW(int dow);
extern int SetRTCHOUR(int hour);
extern int SetRTCMIN(int min);
extern int SetRTCSEC(int sec);

extern int GetRTCTIM0(void); 	
extern int GetRTCTIM1(void); 	
extern int GetRTCDATE(void);
extern int GetRTCYEAR(void);

extern int GetRTCMON(void);
extern int GetRTCDOW(void);
extern int GetRTCDAY(void);
extern int GetRTCHOUR(void);
extern int GetRTCMIN(void);
extern int GetRTCSEC(void);
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
void setupRTC(int Year, int Month, int Day, int Hour, int Minute, int Second);
void getClockUpdate(void);
void setDailyCounterStart(void (*ptr_func)());
void DisableDailyCounter();



unsigned int getHour();
unsigned int getMin();
unsigned int getSec();

unsigned int getYear();
unsigned int getMonth();
unsigned int getDay();


////////////////////////////////////////////////////////////////////////////
//  
//  Button configuration
//  Switch P2.3, press = high, releae = low
////////////////////////////////////////////////////////////////////////////

#define BUTTON_PORT_IN          P2IN
#define BUTTON_PORT_DIR			    P2DIR
#define BUTTON_PORT_SEL			    P2SEL
#define BUTTON_PORT_IE          P2IE
#define BUTTON_PORT_IES         P2IES
#define BUTTON_PORT_IFG         P2IFG
#define BUTTON_PORT_VECTOR      PORT2_VECTOR

#define SW1_BUTTON     		    BIT3
//#define SW1_BUTTON     		    BIT4 // for demo board (up)
void InitButton();
unsigned char getSW1Status();
void Enable_SW_Interrupt(unsigned char enable);


////////////////////////////////////////////////////////////////////////////
//Timer B configuration
////////////////////////////////////////////////////////////////////////////
void InitTimerB();
void setTimerInterrupt(char enable);
//void setTimerInterrupt();
//void setCCRInterrupt();
//void setTimerFunc(int (*ptr1)(char enable), char startStatus, unsigned int delay50ms_count);
//void setTimerFuncForBlink(void (*ptr1)(char enable, char leftData, char rightData),char startStatus ,char leftData, char rightData, unsigned int delay50ms_count){
void setTimerFuncForBlink(void (*ptr1)(char enable), char startStatus, unsigned int delay50ms_count);
void disableTimerFuncForBlink();
void setButtonTimerToRunFun(void (*ptr1)(), unsigned int delay50ms_count);
void setButtonTimer(unsigned int delay50ms_count);
void disableButtonTimer();
void setBalanceTimerToRunFunc(void (*ptr1)(), unsigned int delay50ms_count);
void disableBalanceTimer();
void setProtectionTimerToRunFunc(void (*ptr1)(), unsigned int delay50ms_count);
void disableProtectionTimer();
//Over Current counting Timer
void setOverCurrentCountingTimerToRunFunc(void (*ptr1)());
void DisableOverCurrentCountingFunc();


////////////////////////////////////////////////////
//Watch Dog function
void setWatchDogTimerToRunFunc(void (*watchDogPtr)(), unsigned int delay50ms_count);
void StartWatchDog();
void StopWatchDog();
void DisableWatchDogFunc();



//#define _TimerCounterOn_
#ifdef _TimerCounterOn_
void settf();
void stoptf();
void cleartfc();
#endif








